// const { proxy } = getCurrentInstance() as ComponentInternalInstance;
export const dealText = (option: { [key: string]: any }, content: string, replaceValue: string) => {
	const { insertType, rowType, excludeRow, aimRow, aimIndex, aim } = option;
	const lineList = content.replace(/\r/gi, '').split('\n');
	const afterList: Array<string> = [];
	for (const index in lineList) {
		const line = lineList[index];
		const lineNum = Number(index) + 1;
		if (rowType === 1) {
			//改变奇数行
			if (lineNum % 2 == 0) {
				//偶数行放行
				afterList.push(line);
				continue;
			}
		}
		if (rowType === 2) {
			//改变偶数行
			if (lineNum % 2 != 0) {
				//奇数行放行
				afterList.push(line);
				continue;
			}
		}
		if (excludeRow) {
			if (excludeRow.trim().length) {
				//存在排除行数的设置
				const excludeRowStr = excludeRow.trim();
				const excludeRowList = excludeRowStr.split('/');
				if (excludeRowList.indexOf(lineNum + '') > -1) {
					afterList.push(line);
					continue;
				}
			}
		}
		if (aimRow) {
			//存在特定行形态的设置
			if (line.indexOf(aimRow) == -1) {
				afterList.push(line);
				continue;
			}
		}
		//开始执行 需要区分替换和插入 存在insertType为插入
		if (aimIndex) {
			//存在每行执行目标序列
			const index = Number(aimIndex);
			if (typeof index == 'number') {
				//只替换指定序列
				const lineAimCount = line.split(aim).length - 1;
				if (index > lineAimCount) {
					ElMessageBox.alert('行内序列:' + index + ' 超出行内目标最少出现次数:' + lineAimCount, '系统提示', { type: 'error' });
					return;
				}
				if (lineAimCount > 1) {
					let dealIndex = 0;
					let cacheLine = line;
					let holdnum = 0;
					for (let i = 1; i <= index; i++) {
						dealIndex = cacheLine.indexOf(aim) + holdnum;
						cacheLine = cacheLine.replace(aim, '');
						holdnum += aim.length;
					}
					//执行
					afterList.push(line.substr(0, dealIndex) + replaceValue + line.substr(dealIndex + aim.length));
				}
			} else {
				ElMessageBox.alert('行内序列只能是数字', '系统提示', { type: 'error' });
				return;
			}
		} else {
			//不存在每行执行序列
			if (insertType === 2) {
				afterList.push(replaceValue + line);
			}
			if (insertType === 3) {
				afterList.push(line + replaceValue);
			}
			if (!insertType || insertType == 1) {
				afterList.push(line.replaceAll(aim, replaceValue));
			}
		}
	}
	return afterList.join('\n');
};

export const formateJson = (content: string) => {
	content = content.replaceAll(' ', '').replaceAll('\n\t', '').replaceAll('\n', '').replaceAll('\t', '');
	const bigBracketsLeftCount = content.split('{').length - 1;
	const bigBracketsRightCount = content.split('}').length - 1;
	const middleBracketsLeftCount = content.split('[').length - 1;
	const middleBracketsRightCount = content.split(']').length - 1;
	const doubleSCount = content.split('"').length - 1;
	if (bigBracketsLeftCount != bigBracketsRightCount) {
		const num = Math.abs(bigBracketsLeftCount - bigBracketsRightCount);
		ElMessageBox.alert(bigBracketsLeftCount > bigBracketsRightCount ? '右侧缺少' + num + '个大括号' : '左侧缺少' + num + '个大括号', '系统提示', { type: 'error' });
		return;
	}
	if (middleBracketsLeftCount != middleBracketsRightCount) {
		const num = Math.abs(middleBracketsLeftCount - middleBracketsRightCount);
		ElMessageBox.alert(middleBracketsLeftCount > middleBracketsRightCount ? '右侧缺少' + num + '个中括号' : '左侧缺少' + num + '个中括号', '系统提示', { type: 'error' });
		return;
	}
	if (doubleSCount % 2 != 0) {
		ElMessageBox.alert('缺少双引号', '系统提示', { type: 'error' });
	}
	if (content[0] == '"' || content[content.length - 1] == '"') {
		ElMessageBox.alert('正确的json不以双引号开头或结尾', '系统提示', { type: 'error' });
		return;
	}
	if(content.indexOf('\'')>-1){
		ElMessageBox.alert('正确的json不可存在单引号', '系统提示', { type: 'error' });
		return;
	}
	let newJson = '';
	let level = 0; //定义层级，0为最外层不需要偏移，每个层级便宜4个空格
	for (let i = 0; i < content.length; i++) {
		const pre = i > 0 ? content[i - 1] : '\0'; //前一个字符
		const now = content[i]; //当前字符
		const after = i < content.length - 1 ? content[i + 1] : '\0'; //后一个字符
		//如果是第一个字符，则写入后结束
		if (i == 0) {
			newJson += now;
			continue;
		}
		//如果前一个字符是开括号，则换行并且增加层级后，进行偏移
		if (pre == '{' || pre == '[') {
			level++;
			newJson += '\r\n'; //回车
			//追加空格
			for (let ii = 0; ii < level * 2; ii++) {
				newJson += ' ';
			}
		}
		//如果前一个字符是逗号，则不增加层级的换行
		if (pre == ',') {
			newJson += '\r\n'; //回车
			for (let ii = 0; ii < level * 2; ii++) {
				newJson += ' ';
			}
		}
		//如果当前字符是]},则减少层级后换行
		if (now == '}' || now == ']') {
			level--;
			newJson += '\r\n'; //回车
			for (let ii = 0; ii < level * 2; ii++) {
				newJson += ' ';
			}
		}
		newJson += now;
	}

	return newJson;
};
